/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pk1;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;

/**
 *
 * @author nghiapt
 */
public class CleanerAgent extends Agent{
    
    private int x,y;    // Tọa độ x, y tương ứng của agent
    private int vision; // Tầm nhìn của agent
    private int[][] map = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},};  // bản đồ có kích thước 20 * 20, mỗi ô có kích thước là 4
    private AID[] cleanerAgents;
    @Override
    protected void setup(){
        setVision(4);
        setXY(400, 400);
        System.out.println("I'm agent " + getAID().getLocalName());
        
        // Đăng kí dịch vụ Current với yelowpage
        // Dịch vụ này cung cấp thông tin về tọa độ (x, y) hiện tại của cleanerAgent
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        ServiceDescription sd = new ServiceDescription();
        sd.setType("current");
        sd.setName("JADE-current");
        dfd.addServices(sd);
        try {
                DFService.register(this, dfd);
        }
        catch (FIPAException fe) {
                fe.printStackTrace();
        }
        
        //lập lịch làm việc thông qua ticker behaviour
        addBehaviour(new TickerBehaviour(this, 500) {

            @Override
            protected void onTick() {
                addBehaviour(new MoveBehaviour());
                // Use current service: check if see a agent
                DFAgentDescription template = new DFAgentDescription();
                ServiceDescription sd = new ServiceDescription();
                sd.setType("current");
                template.addServices(sd);
                try {
                        DFAgentDescription[] result = DFService.search(myAgent, template); 
                        System.out.println("Found the following cleaner agents:");
                        cleanerAgents = new AID[result.length];
                        for (int i = 0; i < result.length; ++i) {
                                cleanerAgents[i] = result[i].getName();
                                System.out.println(cleanerAgents[i].getName());
                        }
                }
                catch (FIPAException fe) {
                        fe.printStackTrace();
                }
                //addBehaviour(new CheckNearAgentBehaviour());
            }
        });
    }
    @Override
    protected void takeDown(){
        // Deregister from the yellow pages
        try {
            DFService.deregister(this);
        }
        catch (FIPAException fe) {
            fe.printStackTrace();
        }
    }
    
    public void update(){
        
    }
    public void draw(Graphics2D g2d){
        ImageIcon ic = new ImageIcon("D:/nghiapt/AgentProj/Agent/src/pictures/cleaner.jpg");
        Image i = ic.getImage();
        g2d.drawImage(i, x, y , null);
    }
    public void setXY(int x, int y){
        this.x = x;
        this.y = y;
    }
    public int getX(){
        return x;
    }
    public int getY(){
        return y;
    }
    public void setVision(int v){
        this.vision = v;
    }
    public void setMap(int[][] map){
        this.map = map;
    }
    public int[][] getMap(){
        return this.map;
    }
    public void setMapGone(int x, int y){
        int mX = 0; //mapX
        int mY = 0; //mapY
        mX = x/20;
        mY = y/20;
        this.map[mY][mX] = 1;
    }
    

    /**
     * Đặt các lớp hành vi vào trong các Agent, khi chạy sẽ nhanh hơn
     * Có thể sử dụng CycleBehaviour hoặc OneshotBehavior
     */
    private class MoveBehaviour extends OneShotBehaviour{
        //0 North
        //1 Est
        //2 Sounth
        //3 West
        @Override
        public void action() {
            int r = 0;
            Random random = new Random();
            if(x>=0 && x<= 800 && y>=0 && y<=800){
                r = (int)random.nextInt(4);
                System.out.println(r);
                switch(r){
                    case 0:
                        y -= 4;
                        break;
                    case 1:
                        x += 4;
                        break;
                    case 2:
                        y += 4;
                        break;
                    case 3:
                        x -= 4;
                        break;
                }
            }
            //setMapGone(x,y);
            System.out.println("x = " + x + "; y= " + y);
        }
    }
    private class CleanBehaviour extends CyclicBehaviour{
        @Override
        public void action() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }    
    
    /**
     * Thực hiện 1 lần khi bắt đầu tham gia dọn dẹp
     * 
     */
    private class StartBehaviour extends OneShotBehaviour{
        @Override
        public void action() {
        }        
    }
    
    private class UpdateMapBehaviour extends OneShotBehaviour{
        @Override
        public void action() {
            
        }        
    }
    
    private class CheckNearAgentBehaviour extends Behaviour{
        private int step = 0;
        @Override
        public void action() {
            switch (step){
                case 0:
                    // Send the cfp to all cleaner
                    ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
                    for (int i = 0; i < cleanerAgents.length; ++i) {
                            cfp.addReceiver(cleanerAgents[i]);
                    } 
                    cfp.setContent("getXY");
                    cfp.setConversationId("book-trade");
                    cfp.setReplyWith("cfp"+System.currentTimeMillis()); // Unique value
                    myAgent.send(cfp);
            }
        }

        @Override
        public boolean done() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
    }
    
}
